package com.yuefresh.util;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.yuefresh.sys.User;

/**
 * request相关操作辅助类
 * 
 * @author chnxan
 * @version 1.0 2013-6-8 chnxan
 */
public class RequestUtil {

private final static Logger logger = LoggerFactory.getLogger(RequestUtil.class);
	
	/**
	 * 通过request得到session
	 * 
	 * @param request
	 * @return
	 */
	public static HttpSession getSession(HttpServletRequest request) {
		return 	request.getSession();
	}

	/**
	 * 获得session范围内的属性值
	 * @param request
	 * @param name
	 * @return
	 */
	public static Object getSessionAttribute(HttpServletRequest request, String name){
		return getSession(request).getAttribute(name);
	}
	/**
	 * 设定session范围内的属性值
	 * @param request
	 * @param name
	 * @return
	 */
	public static void setSessionAttribute(HttpServletRequest request, String name, Object value){
		getSession(request).setAttribute(name, value);
	}
	
	/**
	 * 从session中获得保存的登录后的用户相关信息资料
	 * 
	 * @param request
	 * @return
	 */
	public static User getUser(HttpServletRequest request){
		User user= (User)getSession(request).getAttribute("login_user");
	    Assert.notNull(user, "login_user is null, please re-login.");
		return user;
	}
	

	/**
	 * Null字符串处理成""
	 * @param paraName
	 * @return
	 */
	public static String getString(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		if (value == null) {
			return "";
		} else {
			return value.trim();
		}
	}
	
	/**
	 * 取得日期型参数
	 * @return
	 */
	public static Date getDate(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		if (StringUtil.isEmpty(value)) return null;
		Date date = DateUtil.convert(DateUtil.getDatePattern(), value);
		return date;
	}
	
	/**
	 * 取Long，如果为空返回0L
	 * @param request
	 * @param paraName
	 * @return
	 */
	public static Long getLong(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		
		if (StringUtil.isEmpty(value)) {
			return 0L;
		} else {
			return new Long(value.trim());
		}
	}
	public static Integer getInteger(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		
		if (StringUtil.isEmpty(value)) {
			return 0;
		} else {
			return new Integer(value.trim());
		}
	}
	public static Double getDouble(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		
		if (StringUtil.isEmpty(value)) {
			return 0.0;
		} else {
			return new Double(value.trim());
		}
	}
	
	public static Double getNumber(HttpServletRequest request, String paraName) {
		String value = request.getParameter(paraName);
		
		if (StringUtil.isEmpty(value)) {
			return 0.0;
		} else {
			value = StringUtil.replace(value, ",", "");
			return new Double(value.trim());
		}
	}
	
	/**
	 * 从request中获取paramName变量名的字符串，并将结果转换为String 类型，用于作批量 操作（如：删除、作废）
	 * 通过[,] 分段
	 * @param request
	 * @param paramName
	 * @return
	 */
	public static String[] getStrings(HttpServletRequest request, String paraName) {
		String[] values = request.getParameterValues(paraName);

		return values;
	}
	
	/**
	 * 从request中获取id字符串，并将结果转换为long 类型，用于作批量 操作（如：删除、作废）
	 * @param request
	 * @return
	 */
	public static Long[] getLongs(HttpServletRequest request, String paraName) {
		String[] values = request.getParameterValues(paraName);
		
		Long[] lvalues = new Long[values.length];
		
		for (int i = 0; i < lvalues.length; i++) {
			lvalues[i] = Long.valueOf(values[i]);
		}
		
		return lvalues;
	}
	
	/**
	 * 页面重定向
	 * @param request
	 * @param response
	 * @param redirectUrl
	 * @throws Exception
	 */
	public static void sendRedirect(HttpServletRequest request, HttpServletResponse response, String redirectUrl) throws Exception {
		
		String url = request.getContextPath() + redirectUrl;
		
		logger.debug("Chint Debug[RequestUtil Log] --> sendRedirect to url[{}]", url);
		response.sendRedirect(url);
		
	}

	/**
	 * 获取form 提交过来的所有参数
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getMap(HttpServletRequest request) {
		Enumeration<?> paramNames = request.getParameterNames();
		Map<String, Object> params = new HashMap<String, Object>();

		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				// Do nothing, no values found at all.
			} else if (values.length > 1) {
				params.put(paramName, values);
			} else {
				params.put(paramName, values[0]);
			}
		}
		return params;
	}
}
